package com.budwk.app.sqfw.services.impl;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.models.Sqfw_WorkSummary;
import com.budwk.app.sqfw.services.SqfwWorkSummaryService;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;
import java.util.List;

@IocBean(args = {"refer:dao"})
public class SqfwSqfwWorkSummaryServiceImpl extends BaseServiceImpl<Sqfw_WorkSummary> implements SqfwWorkSummaryService {
    public SqfwSqfwWorkSummaryServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public void delete(String userId, Date date) {
        Sql sql = Sqls.queryEntity("delete from shequ.sqfw_work_summary where user_id = @userId and fill_date = @date");
        sql.setEntity(super.getEntity());
        sql.params().set("userId", userId);
        sql.params().set("date", date);
        super.execute(sql);
    }

    @Override
    public Pagination getByDate(String userId, String date, int pageNo, int pageSize) {
        Sql sql = Sqls.queryEntity("select user_name,fill_date,url from shequ.sqfw_work_summary " +
                "where user_id = @userId and to_char(fill_date , 'YYYY-MM') = @date group by user_name,fill_date,url ORDER BY fill_date DESC");
        sql.setEntity(super.getEntity());
        sql.params().set("userId", userId);
        sql.params().set("date", date);
        Pagination pagination = super.listPage(pageNo, pageSize, sql);
        List<Sqfw_WorkSummary> workSummaryList = pagination.getList(Sqfw_WorkSummary.class);
        for (Sqfw_WorkSummary workSummary : workSummaryList) {
            workSummary.setCreateDate(DateUtil.offsetDay(workSummary.getFillDate(), 1));
        }
        pagination.setList(workSummaryList);
        return pagination;
    }

    @Override
    public Pagination getWorkSummaryByDate(String userId, String date, int pageNo, int pageSize) {
        Sql sql = Sqls.queryEntity("select * from shequ.sqfw_work_summary " +
                "where user_id = @userId and fill_date = @date order by index asc");
        sql.setEntity(super.getEntity());
        sql.params().set("userId", userId);
        sql.params().set("date", date);
        return super.listPage(pageNo, pageSize, sql);
    }

    @Override
    @Aop(TransAop.READ_COMMITTED)
    public void update(List<Sqfw_WorkSummary> workSummaryList, List<String> idList) {
        super.delete(idList);
        for (Sqfw_WorkSummary workSummary : workSummaryList) {
            workSummary.setUpdateDate(new Date());
            super.updateIgnoreNull(workSummary);
        }
    }

    @Override
    public void update(String userId, Date date, String url) {
        Sql sql = Sqls.queryEntity("update shequ.sqfw_work_summary set url = @url " +
                "where user_id = @userId and fill_date = @date");
        sql.setEntity(super.getEntity());
        sql.params().set("url", url);
        sql.params().set("userId", userId);
        sql.params().set("date", date);
        super.execute(sql);
    }
}
